<?php
/* SVN FILE: $Id: coreExtensions.php 224 2009-09-17 19:25:18Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* Core variables extensions
* Extends functionality to IP.Board
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 224 $
* @lastrevision $Date: 2009-09-17 19:25:18 +0000 (Thu, 17 Sep 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-09-17 19:25:18 +0000 (Thu, 17 Sep 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/extensions/coreExtensions.php $
 **/

class publicSessions__ibmarket
{
	/**
	* Return session variables for this application
	*
	* current_appcomponent, current_module and current_section are automatically
	* stored. This function allows you to add specific variables in.
	*
	* @access	public
	* @return   array
	*/
	public function getSessionVariables()
	{
		if(ipsRegistry::$current_module == '' || ipsRegistry::$current_module == 'shop')
		{
			if(ipsRegistry::$current_section == '' || ipsRegistry::$current_section == 'shop')
			{
				return array( 'location_1_id' => intval(ipsRegistry::$request['category']) );
			}
		}
		elseif(ipsRegistry::$current_module == 'inventory')
		{
			if(ipsRegistry::$current_section == 'resell' || ipsRegistry::$current_section == 'item')
			{
				return array( 'location_1_id' => intval(ipsRegistry::$request['item']) );
			}
		}
		elseif(ipsRegistry::$current_module == 'plugin')
		{
			return array( 'location' => ipsRegistry::$request['plugin'] );
		}
		return array();
	}
	
	
	/**
	* Parse/format the online list data for the records
	*
	* @access	public
	* @param	array 			Online list rows to check against
	* @return   array 			Online list rows parsed
	*/
	public function parseOnlineEntries( $rows )
	{
		if( !is_array($rows) OR !count($rows) )
		{
			return $rows;
		}
		
		ipsRegistry::getClass('class_localization')->loadLanguageFile( array( 'public_market_session' ), 'ibmarket' );
		
		ipsRegistry::cache()->getCache(array('market_items','market_category','market_plugin'));
		$caches =& ipsRegistry::cache()->fetchCaches();
		
		require_once( IPSLib::getAppDir('ibmarket')."/sources/marketRegistry.php");
		ipsRegistry::member()->setProperty('market_deduct', 1);
		ipsRegistry::member()->setProperty('market_tax', 0);
		marketRegistry::__setup();
		
		$final = array();
		$inventory = array();
		
		foreach( $rows as $row )
		{
			if( $row['current_appcomponent'] != 'ibmarket' || $row['current_module'] != 'inventory' )
			{
				continue;
			}
			if( $row['current_section'] == 'resell' || $row['current_section'] == 'item' )
			{
				$inventory[$row['location_1_id']] = TRUE;
			}
		}
		
		if($inventory)
		{
			ipsRegistry::DB()->build(array('select'=>'*','from'=>'market_inventory','where'=>"i_id IN (".implode(", ",array_keys($inventory)).")"));
			$q = ipsRegistry::DB()->execute();
			while($invItem = ipsRegistry::DB()->fetch($q))
			{
				$inventory[$invItem['i_id']] = $invItem;
			}
		}
		
		ipsRegistry::getClass( 'class_localization' )->words['market_shopping'] = sprintf(ipsRegistry::getClass( 'class_localization' )->words['market_shopping'],ipsRegistry::$settings['market_name']);
		ipsRegistry::getClass( 'class_localization' )->words['market_shopping_all'] = sprintf(ipsRegistry::getClass( 'class_localization' )->words['market_shopping_all'],ipsRegistry::$settings['market_name']);
		
		foreach( $rows as $row )
		{
			if( $row['current_appcomponent'] != 'ibmarket' )
			{
				$final[ $row['id'] ]	= $row;
				
				continue;
			}
			
			$row['where_line']		= ipsRegistry::$settings['market_name'];
			$row['where_line_more']	= '';
			$row['where_link']		= 'app=ibmarket';
			$row['_whereLinkSeo']  = '';
			if($row['current_module'] == 'shop' || $row['current_module'] == '')
			{
				if($row['current_section'] == '' || $row['current_section'] == 'shop')
				{
					if(is_array($caches['market_category'][$row['location_1_id']]))
					{
						$row['where_line']		= ipsRegistry::getClass( 'class_localization' )->words['market_shopping'];
						$row['where_line_more']	= $caches['market_category'][$row['location_1_id']]['cat_name'];
						$row['where_link']		= 'app=ibmarket&amp;categoy='.$row['location_1_id'];
						$row['_whereLinkSeo']  = '';
					}
					else
					{
						$row['where_line']		= ipsRegistry::getClass( 'class_localization' )->words['market_shopping_all'];
						$row['where_line_more']	= '';
						$row['where_link']		= 'app=ibmarket';
						$row['_whereLinkSeo']  = '';
					}
				}
				else
				{
					$row['where_line']		= ipsRegistry::getClass( 'class_localization' )->words['market_buying_items'];
					$row['where_line_more']	= '';
					$row['where_link']		= 'app=ibmarket';
					$row['_whereLinkSeo']  = '';
				}
			}
			elseif($row['current_module'] == 'inventory')
			{
				if($row['current_section'] == 'resell')
				{
					if(is_array($inventory[$row['location_1_id']]) && is_array($caches['market_items'][$inventory[$row['location_1_id']]['item']]) && $inventory[$row['location_1_id']]['member_id'] == $row['member_id'])
					{
						$row['where_line']		= ipsRegistry::getClass( 'class_localization' )->words['market_reselling_item'];
						$row['where_line_more']	= $caches['market_items'][$inventory[$row['location_1_id']]['item']]['item_name'];
						$row['where_link']		= '';
						$row['_whereLinkSeo']  = '';
					}
				}
				elseif($row['current_section'] == 'item')
				{
					if(is_array($inventory[$row['location_1_id']]) && is_array($caches['market_items'][$inventory[$row['location_1_id']]['item']]) && $inventory[$row['location_1_id']]['member_id'] == $row['member_id'])
					{
						$row['where_line']		= ipsRegistry::getClass( 'class_localization' )->words['market_use_item'];
						$row['where_line_more']	= $caches['market_items'][$inventory[$row['location_1_id']]['item']]['item_name'];
						$row['where_link']		= '';
						$row['_whereLinkSeo']  = '';
					}
				}
				else
				{
					$row['where_line']		= ipsRegistry::getClass( 'class_localization' )->words['market_viewing_inventory'];
					$row['where_line_more']	= '';
					$row['where_link']		= 'app=ibmarket';
					$row['_whereLinkSeo']  = '';
				}
			}
			elseif($row['current_module'] == 'plugin')
			{
				if(is_array($caches['market_plugin'][$row['location']]))
				{
					$row['where_line']		= ipsRegistry::$settings['market_name'];
					$row['where_line_more']	= $caches['market_plugin'][$row['location']]['name'];
					$row['where_link']		= 'app=ibmarket&amp;plugin='.$row['location'];
					$row['_whereLinkSeo']  = '';
				}
			}
			if(!ipsRegistry::$settings['market_on'])
			{
				$row['where_link'] = '';
				$row['_whereLinkSeo'] = '';
			}
			if(ipsRegistry::member()->member_id == 0 && !ipsRegistry::$settings['market_guests'])
			{
				$row['where_link'] = '';
				$row['_whereLinkSeo'] = '';
			}
			if(!ipsRegistry::member()->getProperty('g_market_view') || !ipsRegistry::member()->getProperty('market_access'))
			{
				$row['where_link'] = '';
				$row['_whereLinkSeo'] = '';
			}
			
			$final[ $row['id'] ]	= $row;
		}
		
		return $final;
	}
}